package com.ice.project.datamining.gui;

import java.awt.BasicStroke;
import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.Component;
import java.awt.Dimension;
import java.awt.Font;
import java.text.DecimalFormat;

import javax.swing.BoxLayout;
import javax.swing.JLabel;
import javax.swing.JList;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JTable;
import javax.swing.SwingConstants;
import javax.swing.table.DefaultTableModel;

import org.jfree.chart.ChartPanel;
import org.jfree.chart.JFreeChart;
import org.jfree.chart.plot.DialShape;
import org.jfree.chart.plot.MeterInterval;
import org.jfree.chart.plot.MeterPlot;
import org.jfree.data.Range;
import org.jfree.data.general.DefaultValueDataset;
import org.jfree.data.general.ValueDataset;

public class ResultPanel extends JPanel {

	private MainWindow parentWindow;
	private JPanel mainPanel;
	private MeterPlot meterplot;
	private JTable jTConfusionMatrix;
	private Component chartpanel;
	private JLabel jlPrecision;

	public MainWindow getParentWindow() {
		return parentWindow;
	}

	public JPanel getMainPanel() {
		return mainPanel;
	}

	public JTable getConfusionMatrix() {
		return jTConfusionMatrix;
	}

	public ResultPanel(MainWindow mw) {
		super(new BorderLayout());
		parentWindow = mw;
		initializeGUI();
	}

	private void createChart(ValueDataset valuedataset, String title,
			Dimension dim) {
		meterplot = new MeterPlot(valuedataset);
		meterplot.setRange(new Range(0.0D, 100));
		meterplot.addInterval(new MeterInterval("Critical",
				new Range(0.0, 40D), Color.red, new BasicStroke(2.0F),
				new Color(255, 0, 0, 128)));
		meterplot.addInterval(new MeterInterval("Moderate", new Range(40, 60D),
				Color.yellow, new BasicStroke(2.0F), new Color(255, 252, 173)));
		meterplot.addInterval(new MeterInterval("Normal", new Range(60D, 100),
				Color.green, new BasicStroke(2.0F), new Color(0, 255, 0, 64)));

		meterplot.setNeedlePaint(Color.darkGray);
		meterplot.setDialBackgroundPaint(Color.white);
		meterplot.setDialOutlinePaint(Color.black);
		meterplot.setDialShape(DialShape.CHORD);
		meterplot.setMeterAngle(180);
		meterplot.setUnits("%");
		meterplot.setTickLabelsVisible(true);
		meterplot.setTickLabelFont(new Font("Arial", 1, 14));
		meterplot.setTickLabelPaint(Color.black);
		meterplot.setTickSize(5D);
		meterplot.setTickPaint(Color.gray);
		meterplot.setValuePaint(Color.black);
		meterplot.setValueFont(new Font("Arial", 1, 14));
		JFreeChart jfreechart = new JFreeChart(title,
				JFreeChart.DEFAULT_TITLE_FONT, meterplot, true);
		jfreechart.removeLegend();

		chartpanel = new ChartPanel(jfreechart);
		chartpanel.setVisible(false);
		chartpanel.setPreferredSize(dim);
		chartpanel.setSize(dim);
	}

	public MeterPlot getMeterplot() {
		return meterplot;
	}

	public void setMeterplot(MeterPlot meterplot) {
		this.meterplot = meterplot;
	}

	private void initializeGUI() {
		mainPanel = new JPanel();
		mainPanel.setLayout(new BoxLayout(mainPanel, BoxLayout.Y_AXIS));

		JLabel title = new JLabel("<html><u>Results (naive bayesien)</u></html>", SwingConstants.CENTER);
		title.setFont(new Font("Arial",Font.BOLD, 18));
		title.setAlignmentX(Component.CENTER_ALIGNMENT);
		mainPanel.add(title);

		JPanel panelResults = new JPanel();
		panelResults.setLayout(new BoxLayout(panelResults, BoxLayout.X_AXIS));
		
		createChart(new DefaultValueDataset(50D), "Training",
				new Dimension(500, 150));
		panelResults.add(chartpanel);
		
		Object colName[] = { "Good client", "Bad client" };
		double[][] cm = parentWindow.getProbability().getConfusionMatrix();
		final Object rows[][] = { { cm[0][0], cm[0][1] },
				{ cm[1][0], cm[1][1] } };
		DefaultTableModel tableModel = new DefaultTableModel(rows, colName);

		jTConfusionMatrix = new JTable(tableModel) {
			public boolean isCellEditable(int rowIndex, int colIndex) {
				return false; 
			}
		};
		jTConfusionMatrix.setAutoResizeMode(JTable.AUTO_RESIZE_OFF);
		jTConfusionMatrix.setRowSelectionAllowed(false);
		jTConfusionMatrix.setCellEditor(null);

		for (int c = 0; c < jTConfusionMatrix.getColumnCount(); c++) {
			jTConfusionMatrix.getColumnModel().getColumn(c)
					.setCellRenderer(new CellRenderer());
		}

		JList rowHeader = new JList(colName);
		rowHeader.setFixedCellWidth(100);
		rowHeader.setFixedCellHeight(jTConfusionMatrix.getRowHeight()
				+ jTConfusionMatrix.getRowMargin());
		rowHeader.setCellRenderer(new RowHeaderRenderer(jTConfusionMatrix));

		JScrollPane pane = new JScrollPane(jTConfusionMatrix);
		pane.setRowHeaderView(rowHeader);

		double precision = parentWindow.getProbability().getPrecision();
		jlPrecision = new JLabel("<html>Précision : <b>"+String.format("%.4f",precision)+" ("+String.format("%.2f",precision*100)+"%)</b></html>");
		
		JPanel infoPanel = new JPanel();
		infoPanel.setLayout(new BoxLayout(infoPanel, BoxLayout.Y_AXIS));
		infoPanel.add(new JLabel("Confusion matrix"));
		infoPanel.add(pane);
		infoPanel.add(jlPrecision);
		
		panelResults.add(infoPanel);
		mainPanel.add(panelResults);

		this.add(mainPanel);
	}

	public JTable getjTConfusionMatrix() {
		return jTConfusionMatrix;
	}

	public Component getChartpanel() {
		return chartpanel;
	}
}
